package com.cargohu.aishouyu.member.service.impl;

import com.cargohu.aishouyu.framework.exception.BusinessException;
import com.cargohu.aishouyu.framework.page.OrderByItem;
import com.cargohu.aishouyu.framework.page.Paging;
import com.cargohu.aishouyu.framework.service.impl.BaseServiceImpl;
import com.cargohu.aishouyu.member.dto.ChatPhraseAddDTO;
import com.cargohu.aishouyu.member.dto.ChatPhraseUpdateDTO;
import com.cargohu.aishouyu.member.entity.ChatPhrase;
import com.cargohu.aishouyu.member.mapper.ChatPhraseMapper;
import com.cargohu.aishouyu.member.req.ChatPhraseRequest;
import com.cargohu.aishouyu.member.resp.ChatPhraseDetailVo;
import com.cargohu.aishouyu.member.resp.ChatPhraseLabelDetailVo;
import com.cargohu.aishouyu.member.resp.ChatPhraseResponse;
import com.cargohu.aishouyu.member.resp.MemberChatPhraseVo;
import com.cargohu.aishouyu.member.service.ChatPhraseLabelService;
import com.cargohu.aishouyu.member.service.ChatPhraseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 聊天常用语 服务实现类
 *
 * @author yww
 * @since 2023-11-28
 */
@Slf4j
@Service
public class ChatPhraseServiceImpl extends BaseServiceImpl<ChatPhraseMapper, ChatPhrase> implements ChatPhraseService {

    @Autowired
    private ChatPhraseMapper chatPhraseMapper;
    @Autowired
    private ChatPhraseLabelService chatPhraseLabelService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addChatPhrase(ChatPhraseAddDTO chatPhraseAddDTO) throws Exception {
        ChatPhrase chatPhrase = new ChatPhrase();
        BeanUtils.copyProperties(chatPhraseAddDTO, chatPhrase);
        return save(chatPhrase);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChatPhrase(ChatPhraseUpdateDTO chatPhraseUpdateDTO) throws Exception {
        Long id = chatPhraseUpdateDTO.getId();
        ChatPhrase chatPhrase = getById(id);
        if (chatPhrase == null) {
            throw new BusinessException("聊天常用语不存在");
        }
        BeanUtils.copyProperties(chatPhraseUpdateDTO, chatPhrase);
        return updateById(chatPhrase);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteChatPhrase(Long id) throws Exception {
        return removeById(id);
    }

    @Override
    public ChatPhraseDetailVo getChatPhraseById(Long id) throws Exception {
        return chatPhraseMapper.getChatPhraseById(id);
    }

    @Override
    public List<MemberChatPhraseVo> getChatPhraseByMemberId(Long memberId) throws Exception {
        List<MemberChatPhraseVo> memberChatPhraseVoList = new ArrayList<>();

        // 查询用户所有聊天常用语及标签列表
        List<ChatPhraseDetailVo> chatPhraseList = chatPhraseMapper.getAllChatPhraseByMemberId(memberId);
        List<ChatPhraseLabelDetailVo> chatPhraseLabelList = chatPhraseLabelService.getAllChatPhraseLabelList();

        if (!CollectionUtils.isEmpty(chatPhraseLabelList)) {
            chatPhraseLabelList.forEach(label -> {
                // 设置标签信息
                MemberChatPhraseVo memberChatPhraseVo = new MemberChatPhraseVo();
                BeanUtils.copyProperties(label, memberChatPhraseVo);
                // 设置标签下常用语
                List<ChatPhraseDetailVo> tempChatPhraseList = new ArrayList<>();
                chatPhraseList.forEach(chatPhrase -> {
                    if (label.getLabelId().equals(chatPhrase.getLabelId())) {
                        tempChatPhraseList.add(chatPhrase);
                    }
                });
                memberChatPhraseVo.setChatPhraseList(tempChatPhraseList);
                memberChatPhraseVoList.add(memberChatPhraseVo);
            });
        }

        return memberChatPhraseVoList.stream().filter(s -> s.getChatPhraseList().size() > 0).collect(Collectors.toList());
    }

    @Override
    public List<ChatPhraseDetailVo> getAllChatPhraseByMemberId(Long memberId) throws Exception {
        return chatPhraseMapper.getAllChatPhraseByMemberId(memberId);
    }

    @Override
    public Paging<ChatPhraseResponse> getChatPhraseList(ChatPhraseRequest chatPhraseRequest) throws Exception {
        handlePage(chatPhraseRequest, OrderByItem.desc("id"));
        List<ChatPhraseResponse> list = chatPhraseMapper.getChatPhraseList(chatPhraseRequest);
        Paging<ChatPhraseResponse> paging = new Paging<>(list);
        return paging;
    }

}
